package ee.android.kinokavad.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import ee.android.kinokavad.R;
import ee.android.kinokavad.data.DrawerItem;

public class NavigationAdapter extends ArrayAdapter<DrawerItem>{
	
	private LayoutInflater inflater;
	private List<DrawerItem> navigationItems;

	public NavigationAdapter(Context context, List<DrawerItem> navigationItems) {
		super(context, R.layout.drawer_row, navigationItems);
		this.navigationItems = navigationItems;
		this.inflater = LayoutInflater.from(context);
	}
	
	private static class ViewHolder {
		
		private TextView title;
		private ImageView selector;
		private View divider;
		
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.drawer_row, null);
			holder.selector = (ImageView) convertView.findViewById(R.id.navigationRowSelector);
			holder.title = (TextView) convertView.findViewById(R.id.navigationRowTitle);
			holder.divider = convertView.findViewById(R.id.navigationRowDivider);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		DrawerItem navItem = navigationItems.get(position);
		holder.title.setText(navItem.getTitle());
		
		if (navItem.isSelected()) {
			holder.selector.setImageResource(R.color.holo_green_light);
		} else {
			holder.selector.setImageResource(R.color.holo_blue_light);
		}
		
		if (position == navigationItems.size()-1) {
			holder.divider.setVisibility(View.INVISIBLE);
		} else {
			holder.divider.setVisibility(View.VISIBLE);
		}
		
		return convertView;
		
	}

}
